package com.example.yiqingwaichu;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {
    private TextView tv_register;
    private EditText password,number,niem;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        tv_register = findViewById(R.id.tv_register);
        password = findViewById(R.id.password);
        number = findViewById(R.id.number);
        niem=findViewById(R.id.niem);

        tv_register.setOnClickListener(v -> {
            String pass = password.getText().toString();
            String num = number.getText().toString();
            String niem1 = niem.getText().toString();
            if (pass.isEmpty()&&num.isEmpty()){
                Toast.makeText(this,"请检查账号和密码是否填写",Toast.LENGTH_SHORT).show();
                return;
            }

            tv_register.setEnabled(false);
            tv_register.setText("正在注册...");

            String url = "http://www.lefx.top//api/base/register?mobile="+num+"&password="+pass+"&nickname="+niem1;
            OkGo.<String>get(url).execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {

                    try {
                        JSONObject jsonObject = new JSONObject(response.body());
                        int code  = jsonObject.getInt("code");
                        String msg = jsonObject.getString("msg");
                        if (code==1){
                            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                            tv_register.setEnabled(false);
                            tv_register.setText("注册");
                            finish();
                        }else {
                            Toast.makeText(RegisterActivity.this,msg,Toast.LENGTH_SHORT).show();
                        }



                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }




            });


        });




    }
}
